package com.hnevc.broadcastdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    private static final String TAG="MainActivity";
    MyBroadCastReceiver myBroadCastReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void buttonClick(View view) {
        Log.e(TAG, "buttonClick: " );
        int id=view.getId();
        switch (id){
            case R.id.zc:
                IntentFilter intentFilter = new IntentFilter();
                intentFilter.addAction("com.hnevc.BroadCastDemo.PROGRESS");
                myBroadCastReceiver = new MyBroadCastReceiver();
                registerReceiver(myBroadCastReceiver,intentFilter);
                break;
            case R.id.fs:
                Intent intent = new Intent();
                intent.setAction("com.hnevc.BroadCastDemo.PROGRESS");
                intent.putExtra("progress",10);
                sendBroadcast(intent);
                break;
            case R.id.zx:
                unregisterReceiver(myBroadCastReceiver);
                break;
        }
    }

    class MyBroadCastReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            int progress = intent.getIntExtra("progress", 0);
            Log.e(TAG, "onReceive: "+progress );
        }
    }
}
